package com.huilian.iotbox.admin.wap.service.impl;


import com.huilian.iotbox.admin.wap.dao.BankUnionCodeDao;
import com.huilian.iotbox.admin.wap.service.BankUnionCodeService;
import com.huilian.iotbox.data.common.ServerResponse;
import com.huilian.iotbox.data.dto.BankUnionCodeDto;
import com.huilian.iotbox.data.po.BankUnionCode;
import com.huilian.iotbox.data.properties.InterfaceProperties;
import com.huilian.iotbox.data.utils.HttpsMain;
import net.sf.json.JSONArray;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


/**
 * @author 谢洛涛
 * @date 2020/8/29 16:14
 */
@Service
public class BankUnionCodeServiceImpl implements BankUnionCodeService {

    private static String[] bankShortNameArray = new String[]{
            "老河口中银富登村镇",
            "瑞士信贷第一波斯顿",
            "首都银行及信托公司",
            "陕西洛南阳光村镇",
            "太和中银富登村镇",
            "颍上中银富登村镇",
            "意大利联合商业",
            "外汇交易中心",
            "大城舜丰村镇",
            "国债登记结算",
            "鄂尔多斯银行",
            "农村信用联社",
            "德江长征村镇",
            "新津珠江村镇",
            "法国里昂信贷",
            "香港上海汇丰",
            "韩国中小企业",
            "农村信用社",
            "城市信用社",
            "广西北部湾",
            "比利时联合",
            "新加坡发展",
            "奥地利中央",
            "香港清算行",
            "澳门清算行",
            "宁夏银行",
            "华融湘江",
            "南洋商业",
            "厦门国际",
            "台州银行",
            "德国商业",
            "柳州银行",
            "摩根大通",
            "吉林银行",
            "国家开发",
            "城市商业",
            "江苏银行",
            "江西银行",
            "沧州银行",
            "河北银行",
            "法国兴业",
            "法国外贸",
            "泰华农民",
            "泰安银行",
            "济宁银行",
            "浙商银行",
            "泰隆商业",
            "渣打银行",
            "渤海银行",
            "湖北银行",
            "潍坊银行",
            "澳新银行",
            "珠海南通",
            "瑞穗实业",
            "甘肃银行",
            "盘古银行",
            "盛京银行",
            "美国银行",
            "花旗银行",
            "荷兰合作",
            "荷兰商业",
            "荷兰银行",
            "蒙特利尔",
            "西德意志",
            "贵州银行",
            "邮政储汇",
            "邯郸银行",
            "锦州银行",
            "长安银行",
            "长沙银行",
            "集友银行",
            "青岛银行",
            "韩业银行",
            "三井住友",
            "巴黎国际",
            "浦东发展",
            "东京三菱",
            "中信实业",
            "光大银行",
            "农业发展",
            "邮政储蓄",
            "亚洲商业",
            "兰州银行",
            "其他商业",
            "农村合作",
            "农村商业",
            "北京银行",
            "韩国产业",
            "韩国外换",
            "齐商银行",
            "齐鲁银行",
            "国库处",
            "营业处",
            "进出口",
            "德意志",
            "德富泰",
            "廖创兴",
            "农商",
            "大华",
            "东亚",
            "中信",
            "中原",
            "农业",
            "工商",
            "建设",
            "民生",
            "银联",
            "丰业",
            "交通",
            "兴业",
            "华一",
            "华侨",
            "华商",
            "华夏",
            "友利",
            "山口",
            "平安",
            "华兴",
            "南粤",
            "发展",
            "广发",
            "建东",
            "德州",
            "徽商",
            "恒丰",
            "恒生",
            "成都",
            "招商",
            "新韩",
            "星展",
            "枣庄",
            "民生",
            "永亨",
    };

    @Resource
    private BankUnionCodeDao bankUnionCodeDao;

    public static void main(String[] args) {
        String bankName = "发展银行";
        boolean match = false;
        for (int i = 0; i < bankShortNameArray.length; i++) {
            if (bankName.indexOf(bankShortNameArray[i]) != -1) {
                match = true;
                System.out.println(bankShortNameArray[i]);
                break;
            }
        }
        System.out.println(match);
    }

    /**
     * @param bankUnionCodeDto
     * @return
     * @throws Exception
     */
    @Override
    public ServerResponse getBankUnionCodeV2(BankUnionCodeDto bankUnionCodeDto) throws Exception {

        // 加入银行关键字识别
        for (int i = 0; i < bankShortNameArray.length; i++) {
            if (bankUnionCodeDto.getBankName().indexOf(bankShortNameArray[i]) != -1) {
                bankUnionCodeDto.setBankName(bankShortNameArray[i]);
                break;
            }
        }

        BankUnionCode bankUnionCode = bankUnionCodeDao.getBankUnionCodeBy(bankUnionCodeDto);
        if (bankUnionCode == null && bankUnionCodeDto.getAccountType().equals("01")) {
            if (StringUtils.isBlank(bankUnionCodeDto.getContactLine())) {
                bankUnionCode = getContactLineByJingDongApi(bankUnionCodeDto);
            }
        }
        return ServerResponse.createBySuccess(bankUnionCode);
    }

    private BankUnionCode getContactLineByJingDongApi(BankUnionCode bankCardParam) throws Exception {
        //京东收费查询联行号地址  200元  1万次
        String url = InterfaceProperties.REQ_URL + "?appkey=" + InterfaceProperties.APP_KEY + "&card=";
//        String url = "https://way.jd.com/yingyan/querycnaps?appkey=f4a7d12638df661c4285b68d220417b0&card=";
//        String url = "";
        url = url + bankCardParam.getBankCardNo();
        String response = HttpsMain.httpsReq(url, "");
        System.out.println(response);
        net.sf.json.JSONObject jsonObject = net.sf.json.JSONObject.fromObject(response);
        System.out.println(jsonObject.getString("code"));
        if ("10000".equals(jsonObject.getString("code"))) {
            net.sf.json.JSONObject result = net.sf.json.JSONObject.fromObject(jsonObject.getString("result"));
            net.sf.json.JSONObject data = net.sf.json.JSONObject.fromObject(result.getString("data"));
            try {
                JSONArray record = data.optJSONArray("record");
                if (record != null) {
                    net.sf.json.JSONObject object = net.sf.json.JSONObject.fromObject(record.get(0).toString());
                    //联行号
                    String contactLine = object.getString("bankCode");
                    bankCardParam.setContactLine(contactLine);
                    //支行名称
                    String branchName = object.getString("lName");
                    bankCardParam.setBranchName(branchName);
                    String bankProvince = object.getString("province");
                    bankCardParam.setBankProvince(bankProvince);
                    String bankCity = object.getString("city");
                    bankCardParam.setBankCity(bankCity);
                    String bankName = object.getString("bank");
                    bankCardParam.setBankName(bankName);
                    Integer count = bankUnionCodeDao.findBankUnionByRepeat(bankCardParam);
                    if(count == 0){
                        bankUnionCodeDao.insertBankUnionCodeBy(bankCardParam);
                    }
                } else {
                    //银行名称
                    if (StringUtils.isBlank(bankCardParam.getBankName())) {
                        String bankName = data.getString("bank");
                        bankCardParam.setBankName(bankName);
                    }
                }
            } catch (Exception e) {

            }
        }
        return bankCardParam;
    }
}
